﻿using NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Markup;

namespace playwrightJu3.Core
{
    public class File
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        private Project currentProject;
        public required string Name { get; set; }
        public File(Project project)
        {
            currentProject = project;
        }
        public void Create()
        {
            if (Checkers.IsValidFileName(Name))
            {
                // 创建一个空的 FlowDocument 并序列化为 XAML
                FlowDocument document = new FlowDocument();
                string xaml = XamlWriter.Save(document);
                System.IO.File.WriteAllText(
                    Path.Combine(currentProject.Location, currentProject.Title, Name),
                    xaml,
                    Encoding.UTF8
                );
                logger.Info($"File Created|File \"{ Name}\" was created in the project \"{currentProject.Title}\", which is located in \"{currentProject.Location}\". ");
                currentProject.Open();
            }
        }

        public void Save(TextRange textRange)
        {
            // 获取 FlowDocument
            FlowDocument doc = textRange.Start.Paragraph?.Parent as FlowDocument;
            try
            {
                string xaml = XamlWriter.Save(doc);
                System.IO.File.WriteAllText(Path.Combine(currentProject.Location, currentProject.Title, Name), xaml, Encoding.UTF8);
                logger.Info($"File Saved|File \"{Name}\" was saved in the project \"{currentProject.Title}\", which is located in \"{currentProject.Location}\". ");
            }
            catch (ArgumentNullException)
            {
                logger.Info($"Empty File Saved|File \"{Name}\" was saved in the project \"{currentProject.Title}\", which is located in \"{currentProject.Location}\" as an empty file. ");
                //支持保存空文件
            }


        }

        public FlowDocument Read()
        {
            // 读取文件内容
            string filePath = Path.Combine(currentProject.Location, currentProject.Title, Name);
            string xaml = System.IO.File.ReadAllText(filePath, Encoding.UTF8);

            // 反序列化为 FlowDocument
            FlowDocument document = new FlowDocument();
            using (var stringReader = new StringReader(xaml))
            using (var xmlReader = System.Xml.XmlReader.Create(stringReader))
            {
                try
                {
                    document = (FlowDocument)XamlReader.Load(xmlReader);
                }
                catch (XamlParseException)
                {
                    throw;
                }

            }
            logger.Info($"File Opened|File \"{Name}\" was opened in the project \"{currentProject.Title}\", which is located in \"{currentProject.Location}\". ");
            // 返回 FlowDocument
            return document;
        }
    }
}
